<template>
    <el-container>
        <el-main class="wrapper">
            <section class="panel cms-content">
                <div class="panel-body">
                    <div class="cms-btn-group">
                        <el-button type="success" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                    </div>
                    <div class="cms-search-group">
                        <div class="cms-search-item">
                            <el-input  size="medium" v-model="params.kwd" placeholder="请输入账号或者联系方式" class="input-with-select">
                                <el-button type="primary" @click="search()" slot="append" icon="el-icon-search"></el-button>
                            </el-input>
                        </div>
                    </div>
                    <el-table ref="multipleHandle"
                              v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              tooltip-effect="dark" :data="listData.data"  border>
                        <el-table-column align="center" prop="id" label="编号" width="60"></el-table-column>
                        <el-table-column prop="thumb" label="头像" width="120">
                            <template slot-scope="scope">
                                <img :src="scope.row.avatar ? scope.row.avatar : require('../../assets/defult.jpg')" style="height: 50px;width: 50px;border-radius: 50%"/>
                            </template>
                        </el-table-column>
                        <el-table-column prop="wname" label="微信名" width="150"></el-table-column>
                        <el-table-column prop="username" label="联系人" width="150"></el-table-column>
                        <el-table-column prop="mobile" label="联系方式" width="120"></el-table-column>
                        <el-table-column align="center" prop="status_text" label="状态" width="75">
                            <template slot-scope="scope">
                                <span v-html="scope.row.status_text"></span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="created_at" label="注册日期" width="180"></el-table-column>
                        <el-table-column label="操作" >
                            <template slot-scope="scope">
                                <el-button @click="edit(scope.row)" icon="fa fa-edit" type="primary" size="mini" plain> 编辑</el-button>
                                <el-button @click="destory(scope.row)" icon="fa fa-trash" type="danger" size="mini" plain> 删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination :pageData="listData" :params="params" @list="list"/>
                </div>
            </section>
        </el-main>
        <!-- 表单弹窗-->
        <el-dialog
                v-if="dialog.show"
                :title="dialog.title"
                custom-class="cms-dialog"
                :top="dialog.top"
                :visible.sync="dialog.show"
                :width="dialog.width">
            <el-form :model="form" :rules="rules" ref="form" label-width="100px">
                <el-form-item label="微信头像：" prop="avatar">
                    <img :src="form.avatar" style="height: 55px;width: 55px;border-radius: 50%"/>
                </el-form-item>
                <el-form-item label="微信名称：" prop="wname">
                    <el-input disabled v-model="form.wname"></el-input>
                </el-form-item>
                <el-form-item label="联系人：" prop="username">
                    <el-input v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item label="联系方式：" prop="mobile">
                    <el-input v-model="form.mobile"></el-input>
                </el-form-item>
                <el-form-item label="注册时间：" prop="created_at">
                    <el-input disabled v-model="form.created_at"></el-input>
                </el-form-item>
                <el-form-item label="状态：" prop="status">
                    <el-radio-group v-model="form.status" size="small">
                        <el-radio :label="1" border>正常</el-radio>
                        <el-radio :label="2" border>锁定</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialog.show = false">取 消</el-button>
                <el-button type="primary" @click="update()">确 定</el-button>
              </span>
        </el-dialog>
    </el-container>
</template>

<script>
    import {mapGetters} from 'vuex';
    import {UserRepairList,UserUpdate,UserDestroy} from '@/utils/request';
    import Pagination from '@/components/pagination';
    export default {
        components: {Pagination},
        data() {
            return {
                listData: [],//列表数据
                listCate: [],
                params: {page:1,kwd:''},
                dialog: {
                    title: '',
                    top: '10vh',
                    show: false,
                    width: '500px'
                },
                form:null,
            }
        },
        activated(){
            this.list();
        },
        computed: {
            ...mapGetters(['active','loading']),
        },
        methods: {
            //编辑
            edit(info){
                this.form = {
                    id: info.id,
                    wname: info.wname,
                    avatar: info.avatar,
                    username: info.username,
                    mobile: info.mobile,
                    status: info.status,
                    created_at: info.created_at,
                };
                this.dialog.title = '编辑维修人员';
                this.dialog.show = true;
            },
            //筛选
            search(){
                this.params.page = 1;
                this.list();
            },
            //列表
            list(){
                this.$store.dispatch('setLoading', true);
                UserRepairList({params: this.params}).then((res)=>{
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.listData = res.data;
                    }
                    this.$store.dispatch('setLoading', false);
                })
            },
            //更新
            update(){
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        UserUpdate(this.form).then((res)=>{
                            if(res.code){
                                this.$message.error(res.msg);
                            }else{
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: res.msg,
                                    onClose:() =>{
                                        this.dialog.show = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            //删除
            destory(info){
                this.$confirm('确定要删除该维修人员信息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    UserDestroy({id: info.id}).then((res) => {
                        if (res.code) {
                            this.$message.error(res.msg);
                        } else {
                            this.list();
                        }
                    });
                }).catch(() => {});
            },
        }
    }
</script>

<style scoped>

</style>
